//const { getbooks } = require("../../router-handler");

$(function() {
    //渲染图书列表
    function getBooks() {
        $.get('http://127.0.0.1:8080/api/getbooks', (res) => {
            let { status, msg, data } = res;
            if (status != 200) {
                alert(msg);
                return;
            }
            let htmlStr = template('tpl', res);
            $('#booklist').html(htmlStr);
        })

    }

    getBooks()

    //删除图书
    //1、点击删除按钮删除书籍
    $('#booklist').on('click', '.del', function() {
            console.log(this);
            let index = $(this).parents('tr').children('td').eq(1).text();
            arr.push(index);
            console.log(arr);
            $.get('http://127.0.0.1:8080/api/delbook', {
                id: arr,
            }, res => {
                let { status, msg } = res;
                if (status === 200) {
                    getBooks();
                } else {
                    alert(msg);
                }
            })
        })
        //2、全选反选
    var arr = [];
    $('#table').on('change', '.a-checkbox', function() {
        $(".j-checkbox, .a-checkbox").prop('checked', $(this).prop('checked'));
        for (var i = 0; i < $(".j-checkbox:checked").length; i++) {
            var indexall = $(".j-checkbox:checked").eq(i).parents('tr').find('.id').text();
            arr.push(indexall);
        }
    })

    $('#booklist').on('change', '.j-checkbox', function() {
        var index = $(this).parents('tr').children('td').eq(1);
        arr.push(index.text());
        if ($(".j-checkbox:checked").length == $(".j-checkbox").length) {
            $(".a-checkbox").prop("checked", true);
        } else {
            $(".a-checkbox").prop("checked", false);
        }
    })

    //3、删除选中的商品
    $('#checkdell').on('click', function() {
        console.log(arr);
        $.get('http://127.0.0.1:8080/api/delbook', {
            id: arr,
        }, res => {
            let { status, msg } = res;
            if (status === 200) {
                getBooks();
            } else {
                alert(msg);
            }
        })
    })

    //添加图书
    $('#addbtn').on('click', function() {
        const bookname = $('#bookname').val();
        const author = $('#author').val();
        const publisher = $('#publisher').val();
        if (bookname.length == 0 || author.length == 0 || publisher.length == 0) {
            alert('请完整填写图书信息！')
        } else {
            $.post('http://127.0.0.1:8080/api/addbook', {
                bookname,
                author,
                publisher,
            }, res => {
                let { status, msg } = res;
                if (status === 201) {
                    getBooks();
                    $('input[type=text]').val('');
                    $('.a-checkbox').attr('checked', false);
                } else {
                    alert(msg);
                }
            })
        }
    })

    //查询图书
    $('#querybtn').on('click', function() {
        const bookname = $('#bookname').val();
        const author = $('#author').val();
        const publisher = $('#publisher').val();
        const datas = {};
        if (bookname.length !== 0) {
            datas.bookname = bookname;
        }
        if (author.length !== 0) {
            datas.author = author;
        }
        if (publisher.length !== 0) {
            datas.publisher = publisher;
        }
        console.log(datas);
        if (bookname.length == 0 && author.length == 0 && publisher.length == 0) {
            alert('请完整填写图书信息！')
        } else {
            $.post('http://127.0.0.1:8080/api/querybook', datas, (res) => {
                let { status, msg, data } = res;
                if (status != 201) {
                    alert(msg);
                    return;
                }
                let htmlStr = template('tpl', res);
                $('#booklist').html(htmlStr);
            })
        }
    })

    //修改图书信息
    var flag = false;
    $('#booklist').on('click', '.edit', function() {
        if (flag == false) {
            console.log($(this).text('确认'));
            const bookname = $(this).parents('tr').children('td').eq(2);
            const author = $(this).parents('tr').children('td').eq(3);
            const publisher = $(this).parents('tr').children('td').eq(4);
            var input = $('<input type = "text" />');
            var inputau = $('<input type = "text" />');
            var inputpu = $('<input type = "text" />');
            input.val(bookname.text());
            inputau.val(author.text());
            inputpu.val(publisher.text());
            bookname.empty().append(input);
            author.empty().append(inputau);
            publisher.empty().append(inputpu);
            flag = true;
        } else {
            const bookname = $(this).parents('tr').children('td').eq(2).find('input').val();
            console.log(bookname);
            const author = $(this).parents('tr').children('td').eq(3).find('input').val();
            console.log(author);
            const publisher = $(this).parents('tr').children('td').eq(4).find('input').val();
            console.log(publisher);
            const id = $(this).parents('tr').children('td').eq(1).text();
            console.log(id);
            if (bookname.length == 0 || author.length == 0 || publisher.length == 0) {
                alert('请完整填写图书信息！')
            } else {
                $.post('http://127.0.0.1:8080/api/editbook', {
                    id,
                    bookname,
                    author,
                    publisher,
                }, res => {
                    let { status, msg } = res;
                    if (status === 201) {
                        console.log(res);
                        getBooks()
                    } else {
                        alert(msg);
                    }
                })
            }
            flag = false;
        }
    })

    //点击清除全部图书
    $('#alldell').on('click', function() {
        $.get('http://127.0.0.1:8080/api/alldelbook', res => {
            let { status, msg } = res;
            if (status === 201) {
                getBooks()
            } else {
                alert(msg);
            }
        })
    })













})